package com.rede.didiok.admin.support;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.TypeReference;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.dto.file.PictureDto;
import com.rede.didiok.common.exception.exceptionType.FeignException;
import com.rede.didiok.common.feign.FileFeignClient;
import com.rede.didiok.common.feign.RankFeignClient;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.business.WebUtil;

/**
 * 图片表 服务实现类
 *
 * @author 陌溪
 * @since 2018-09-04
 */
@Service("pictureSupport")
public class PictureSupport {

    @Autowired
    private WebUtil webUtil;

    @Autowired
    private FileFeignClient fileFeignClient;

    @Autowired
    private RankFeignClient rankFeignClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 编辑图片
     * @param pictureDto
     * @return
     */
    public String editPicture(PictureDto pictureDto) {
        // 根据图片uid获取图片
        R pictureResult = fileFeignClient.getPictureById(pictureDto.getUid());
        PictureDto picture;
        if(pictureResult.getCode()==0){
            picture = pictureResult.getData(new TypeReference<PictureDto>(){});
        }else {
            // 获取异常信息
            String msg = pictureResult.getData("msg",new TypeReference<String>(){});
            throw new FeignException("feign调用异常（根据图片uid获取图片）："+msg);
        }
        // 根据 fileUid 更新对应的所有榜单成员，将其中的fileUid全都更换
        R rankResult = rankFeignClient.updateRankMemberPictureByPictureUid(pictureDto.getFileUid());
        if(rankResult.getCode()!=0){
            // 获取异常信息
            String msg = pictureResult.getData("msg",new TypeReference<String>(){});
            throw new FeignException("feign调用异常（根据 fileUid 更新对应的所有榜单成员）："+msg);
        }
        picture.setFileUid(pictureDto.getFileUid());
        picture.setPicName(pictureDto.getPicName());
        picture.setPictureCategoryUid(pictureDto.getPictureCategoryUid());
        fileFeignClient.updatePictureById(picture);
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

}
